from models.user import User, Role, Power
from repository.user import Repository
from repository import RepositoryNotFoundExt

###########
## 业务层
###########

class Service:
    def __init__(self, repository: Repository):
        self.repository = repository
    async def get_user(self, user_id: int) -> User:
        return await self.repository.get_user(user_id)
    
    async def get_users(self, page_size: int = 10, page_number: int = 1) -> list[User]:
        offset = page_size * (page_number - 1)
        limit = page_size * page_number
        return await self.repository.get_users(offset,limit)
    
    async def get_user_role_powers(self, user_id: int) -> User:
        return await self.repository.get_user_role_powers(user_id)

    async def has_perm(self, user_id: int, power_name: str) -> bool:
        return await self.repository.has_perm(user_id, power_name)

    async def update_user(self, user_id: int, update_values: dict) -> int:
        return await self.repository.update_user(user_id, update_values)
    
    async def create_user(self, create_values: dict) -> User:
        return await self.repository.create_user(create_values)
    async def get_user_roles(self, user_id: int) -> User:
        return await self.repository.get_user_roles(user_id)
    
    async def get_role(self, role_id: int) -> Role:
        return await self.repository.get_role(role_id)
    
    async def create_role(self, create_values: dict) -> Role:
        return await self.repository.create_role(create_values)
    async def user_append_role(self, user_id: int, role_id: int) -> User:
        return await self.repository.user_append_role(user_id, role_id)
    
    async def get_powers(self, page_size: int = 10, page_number: int = 1) -> list[Power]:
        offset = page_size * (page_number - 1)
        limit = page_size * page_number
        return await self.repository.get_powers(offset, limit)
    async def get_power(self, power_id: int) -> Power:
        return await self.repository.get_power(power_id)

    async def get_role_powers(self, role_id: int) -> Role:
        return await self.repository.get_role_powers(role_id)

    async def create_power(self, create_values: dict) -> Power:
        return await self.repository.create_power(create_values)

    async def role_append_power(self, role_id: int, power_id: int) -> Role:
        return await self.repository.role_append_power(role_id, power_id)
    